<template>
  <a-tree-select
    v-bind="$attrs"
    v-model="currentValue"
    :treeData="treeData"
    :replaceFields="{ children: 'children', title: 'label', key: 'id',value:'id' }"
    :dropdown-style="{ maxHeight: '400px', overflow: 'auto' }"
    placeholder="请选择"
    @select="onSelect"
  >
  </a-tree-select>
</template>

<script>
import { getCustOrgTree } from '@/api/subassembly/TreeSelectOrganization'

/*
* */
export default {
  // v-model绑定的数据
  props: {
    value: {
      type: [Array, String, Object]
    },
    autoSelect: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      treeData: []
    }
  },
  created() {
    this.getCompanyList()
  },
  methods: {
    onSelect(e, node) {
      let child = node.$vnode.data.props
      this.$emit('onSelectChange', { id: e, name: child.title })
    },
    async getCompanyList() {
      if (this.$currentPost().code === 'BC') {
        this.treeData = [{ id: this.$currentPost().orgId, label: this.$currentPost().orgName }]
      } else {
        let params = {
            containTags:['CO','BC'],
            baseOrgType:0, 
            systemOrgType: 1, 
            containVirtual: 1, 
        }
        let res = await getCustOrgTree(params)
        this.treeData = res.data
      }
      if (this.autoSelect && this.treeData.length) {
        this.currentValue = this.treeData[0].id
        this.$emit('onSelectChange', {
          id: this.treeData[0].id,
          name: this.treeData[0].label
        })
      }
    }
  },
  computed: {
    // 转换绑定的选中数据，与v-model同步
    currentValue: {
      get: function() {
        return this.value
      },
      set: function(value) {
        // console.log(value)
        this.$emit('input', value)
        this.$emit('change', value)
      }
    }
  }
}
</script>
